using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace HelloXNA2
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Dictionary<Keys, string> _demos;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferWidth = 1280;
            graphics.PreferredBackBufferHeight = 720;


            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            _demos = new Dictionary<Keys, string>();

            _demos.Add(Keys.G, "HelloXNA2.Component.Sprite.AnimatingSprite");
            _demos.Add(Keys.H, "HelloXNA2.Component.Control.KeyboardControl");
            _demos.Add(Keys.I, "HelloXNA2.Component.Control.MouseControl");
            _demos.Add(Keys.K, "HelloXNA2.Component.Audio.XACT");
            _demos.Add(Keys.L, "HelloXNA2.Component.Audio.SoundEffectDemo");
            _demos.Add(Keys.M, "HelloXNA2.Component.Audio.MediaPlayerDemo");
            _demos.Add(Keys.N, "HelloXNA2.Component.Collision.AABB");
            _demos.Add(Keys.O, "HelloXNA2.Component.Collision.RectangleCollision");
            _demos.Add(Keys.P, "HelloXNA2.Component._3D.Basic.Demo");
            _demos.Add(Keys.Q, "HelloXNA2.Component._3D.Basic.Animation");
            _demos.Add(Keys.R, "HelloXNA2.Component._3D._3DModel1.Demo");
            _demos.Add(Keys.S, "HelloXNA2.Component._3D.Collision.Demo");
            _demos.Add(Keys.T, "HelloXNA2.Component.MyCustom.CubeTest");
            _demos.Add(Keys.U, "HelloXNA2.Component.MyCustom.Cube.Demo");
            _demos.Add(Keys.W, "HelloXNA2.Component.MyCustom.FourPolygon.Demo");
            _demos.Add(Keys.X, "HelloXNA2.Component.MyCustom.DrawLine.Demo");
            _demos.Add(Keys.Y, "HelloXNA2.Component.MyCustom.DrawCircle.Demo");
            _demos.Add(Keys.Z, "HelloXNA2.Component.MyCustom.Mouse.Demo");

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }


        private Keys _preKey = Keys.None;
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here

            IGameComponent component;
            Keys[] keys = Keyboard.GetState().GetPressedKeys();

            if (keys.Length > 0 && keys[0] != _preKey)
            {
                if (_demos.ContainsKey(keys[0]))
                {
                    component = (IGameComponent)Activator.CreateInstance(Type.GetType(_demos[keys[0]]), this);
                    Components.Clear();
                    Components.Add(component);
                }

                _preKey = keys[0];
            }


            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }
    }
}
